#pragma once
#include <iostream>
#include "Terrain.h"
#include "animSprite.h"
#include "testUnit.h"

namespace game
{
	class UnitFactory
	{
		game::SpriteSheetManager*	_sprite_sheet_manager;
		game::TerrainManager*		_terrain_manager;
		sf::RenderWindow * _window;

		std::vector<animSpriteData> _sprite_data;
		std::vector<UnitState>	  _default_state;
		std::vector<std::string>  _name;
		//id = array index//
		//animSprite * 
	public:
		UnitFactory(game::SpriteSheetManager * ssm, sf::RenderWindow * window, game::TerrainManager * tm)
		{
			_window = window;
			_sprite_sheet_manager = ssm;
			_terrain_manager = tm;
		}
		const UnitState		get_default_state(std::string name)
		{
			int id = get_index(name);
			return _default_state.at(id);
		}
		const std::string	get_name(unsigned int id)const{return _name.at(id);}
		animSprite *		get_sprite(std::string name)
		{
			
			int id = get_index(name);
			std::cout <<"getting sprite data = "<<_sprite_data.at(id)._anim_ids[0]<<std::endl;
			return new animSprite(_sprite_data.at(id),_sprite_sheet_manager);
		}
		unsigned int get_index(std::string name) const
		{
			const std::size_t size = _name.size();
			for (size_t i = 0; i < size; i++)
			{
				if (_name.at(i) == name){return static_cast<int>(i);}
			}
			return INT_MAX;
		}
		void LoadFactory()
		{
			unsigned int s_anim_id[] = {-1}; 

			s_anim_id[0] = 25; 
			AddUnitType(animSpriteData(0,s_anim_id,1),
						UnitState::S_UNIT_IDLE,"rat");

			s_anim_id[0] = 27; 
			AddUnitType(animSpriteData(0,s_anim_id,1),
						UnitState::S_UNIT_IDLE,"lizard");

			s_anim_id[0] = 33; 
			AddUnitType(animSpriteData(0,s_anim_id,1),
						UnitState::S_UNIT_IDLE,"slime");

			s_anim_id[0] = 18; 
			AddUnitType(animSpriteData(0,s_anim_id,1),
						UnitState::S_UNIT_IDLE,"door");

		}
		void AddUnitType(animSpriteData data, UnitState state, std::string name)
		{
			std::cout << "adding unit with id[0] = " << data._anim_ids[0] << std::endl;
			_sprite_data.push_back(data);
			_default_state.push_back(state);
			_name.push_back(name);
			int index = get_index(name);
			std::cout << "added unit with id[0] = " << (int)_sprite_data.at(index)._anim_ids[0] << std::endl;
		}
		Unit * CreateUnit(std::string name, sf::Vector2f pos)
		{
			std::cout <<"Creating Unit"<<std::endl;
			return new Unit(get_default_state(name),pos,get_sprite(name),_window,name,get_index(name),_terrain_manager);
		}

		Unit const & CreateUnitRef(std::string name, sf::Vector2f pos)
		{
			std::cout <<"Creating Unit"<<std::endl;
			return * (new Unit(get_default_state(name),pos,get_sprite(name),_window,name,get_index(name),_terrain_manager));
		}
	};
}